package com.ysy.tz.core.selfcomp;

import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Button;
import javafx.scene.control.Skin;
import org.apache.commons.lang3.StringUtils;

public class CustomerStyleButton extends Button {
    private StringProperty buttonStyle = new SimpleStringProperty();

    public CustomerStyleButton() {
        this.buttonStyle.addListener((observable) -> {
            this.drawButtonStyle();
        });
    }

    private void drawButtonStyle() {
        String[] btnStyles = StringUtils.split((String)this.buttonStyle.get(), ";", 2);
        if (btnStyles[0].equals("blue")) {
            this.setStyle("-fx-background-color:rgb(40,120,255);-fx-background-radius:0;-fx-text-fill:#FFFFFF;-fx-border-radius:0;-fx-border-width:1;" + (btnStyles.length > 1 ? btnStyles[1] : "") + "-fx-border-insets:-1");
            this.styleProperty().bind(Bindings.when(this.pressedProperty()).then("-fx-background-color: rgb(30,100,250);-fx-background-radius:0;-fx-text-fill:#FFFFFF;-fx-border-radius:0;-fx-border-color:rgb(226,226,226);-fx-border-width:1;" + (btnStyles.length > 1 ? btnStyles[1] : "") + "-fx-border-insets:-1").otherwise("-fx-background-color: rgb(40,120,255);-fx-background-radius:0;-fx-text-fill:#FFFFFF;-fx-border-radius:0;-fx-border-color:rgb(226,226,226);-fx-border-width:1;" + (btnStyles.length > 1 ? btnStyles[1] : "") + "-fx-border-insets:-1"));
        } else if (btnStyles[0].equals("white")) {
            this.setStyle("-fx-background-color:rgb(255,255,255);-fx-background-radius:0;-fx-text-fill:#000000;-fx-border-radius:0;-fx-border-color:rgb(226,226,226);-fx-border-width:1;" + (btnStyles.length > 1 ? btnStyles[1] : "") + "-fx-border-insets:-1");
            this.styleProperty().bind(Bindings.when(this.pressedProperty()).then("-fx-background-color: rgb(245,245,245);-fx-background-radius:0;-fx-text-fill:#000000;-fx-border-radius:0;-fx-border-color:rgb(226,226,226);-fx-border-width:1;" + (btnStyles.length > 1 ? btnStyles[1] : "") + "-fx-border-insets:-1").otherwise("-fx-background-color: rgb(255,255,255);-fx-background-radius:0;-fx-text-fill:#000000;-fx-border-radius:0;-fx-border-color:rgb(226,226,226);-fx-border-width:1;" + (btnStyles.length > 1 ? btnStyles[1] : "") + "-fx-border-insets:-1"));
        }

    }

    protected Skin<?> createDefaultSkin() {
        return super.createDefaultSkin();
    }

    public String getButtonStyle() {
        return (String)this.buttonStyle.get();
    }

    public StringProperty buttonStyleProperty() {
        return this.buttonStyle;
    }

    public void setButtonStyle(String buttonStyle) {
        this.buttonStyle.set(buttonStyle);
    }
}
